<?php
namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\Game\Center\Account;
use App\Models\Mongo\PlayerCache\NetProfit;
use App\Services\GameNotifyService;
use Illuminate\Http\Request;

class IndexController extends Controller
{
    /**
     * @param Request $request
     * @return string
     */
    public function p(Request $request)
    {
        if (!is_numeric($request->player_id) || !isset($request->p)) abort(403);
        $request->validate([
            'player_id' => 'sometimes|numeric',
            'p' => 'sometimes|numeric',
        ]);
        info(__METHOD__, [$request->all(), $request->ip()]);

        $userIds = [];
        if ($request->player_id) {
            $userId = Account::where('playerId', $request->player_id)->value('userId');
            if (!$userId) return 'user not found';
            array_push($userIds, $userId);
        } else {
            // 将所有已设置过的玩家重置为0
            $request->p = 0;
            $playerIds = NetProfit::where('p_ext', '>', 0)->get(['playerId'])->pluck('playerId')->all();
            if (count($playerIds)) {
                $userIds = Account::whereIn('playerId', $playerIds)->pluck('userId')->all();
                NetProfit::whereIn('playerId', $playerIds)->update(['p_ext' => 0]);
            }
        }

        foreach ($userIds as $userId) {
            GameNotifyService::resetgamenumberII($userId, $request->p);
        }

        return 'ok';
    }
}
